package com.lanou.moving.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.moving.R;
import com.lanou.moving.adapter.SingerAdapter;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.SingerRoot;
import com.lanou.moving.fragment.BackHandledFragment;
import com.lanou.moving.fragment.BackHandledInterface;
import com.lanou.moving.fragment.FindFragment;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.VolleySingleton;

import java.util.ArrayList;


/**
 * Created by dllo on 15/10/21.
 */
public class SingerActivity extends BaseActivity implements View.OnClickListener,
        AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {
    private VolleySingleton volleySingleton;
    private GsonRequest<SingerRoot> singerRootGsonRequest;
    private ArrayList<SingerRoot.DataEntity> dataEntityArrayList;
    private SingerAdapter singerAdapter;
    private ListView singer_listview;

    private RadioButton singer_top, singer_recently_hot, singer_chinese,
            singer_western, singer_Janpan_South_Korea;
    private RadioGroup singer_sex;
    private RadioButton singer_man, singer_women, singer_group;
    private TextView singer_item_name;
    private String country, sex;
    private ImageView singer_back, search;//返回

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer);
        BaseApplication.getInstance().addActivity(this);
        country = "c";//初始值
        sex = "m";

        volleySingleton = VolleySingleton.getInstance(this);
        singer_listview = (ListView) findViewById(R.id.singer_listview);
        singer_listview.setOnItemClickListener(this);

        // 返回
        singer_back = (ImageView) findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);

        search = (ImageView) findViewById(R.id.search);
        search.setOnClickListener(this);

        //歌手排名top
        singer_top = (RadioButton) findViewById(R.id.singer_top);
        singer_top.setOnClickListener(this);
        //最近很红
        singer_recently_hot = (RadioButton) findViewById(R.id.singer_recently_hot);
        singer_recently_hot.setOnClickListener(this);
        //华语
        singer_chinese = (RadioButton) findViewById(R.id.singer_chinese);
        singer_chinese.setOnClickListener(this);
        //欧美
        singer_western = (RadioButton) findViewById(R.id.singer_western);
        singer_western.setOnClickListener(this);
        //日韩
        singer_Janpan_South_Korea = (RadioButton) findViewById(R.id.singer_Janpan_South_Korea);
        singer_Janpan_South_Korea.setOnClickListener(this);
        //歌手列表说明
        singer_item_name = (TextView) findViewById(R.id.singer_item_name);
        //歌手性别栏
        singer_sex = (RadioGroup) findViewById(R.id.singer_sex);
        //男歌手
        singer_man = (RadioButton) findViewById(R.id.singer_man);
        singer_man.setOnClickListener(this);
        //女歌手
        singer_women = (RadioButton) findViewById(R.id.singer_women);
        singer_women.setOnClickListener(this);
        //组合
        singer_group = (RadioButton) findViewById(R.id.singer_group);
        singer_group.setOnClickListener(this);

        //返回
        singer_back = (ImageView) findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);
        getSinger(URL.URL_TOP);
    }

    public void getSinger(String url) {
        singerRootGsonRequest = new GsonRequest<SingerRoot>(Request.Method.GET, url, SingerRoot.class, new Response.Listener<SingerRoot>() {
            @Override
            public void onResponse(SingerRoot response) {
                dataEntityArrayList = (ArrayList<SingerRoot.DataEntity>) response.getData();
                singerAdapter = new SingerAdapter(SingerActivity.this, dataEntityArrayList);
                singer_listview.setAdapter(singerAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(singerRootGsonRequest);
    }

    private void showList() {
        singer_item_name.setVisibility(View.GONE);
        singer_sex.setVisibility(View.VISIBLE);
        switch (country + sex) {
            case "cm"://中国男-人
                getSinger(URL.URL_CHINESEBOY);
                break;
            case "cf"://中国女人
                getSinger(URL.URL_CHINESEGIRL);
                break;
            case "cg"://中国组合
                getSinger(URL.URL_CHINESE);
                break;
            case "am"://欧美男歌手
                getSinger(URL.URL_OCCIDENTBOY);
                break;
            case "af"://欧美女歌手
                getSinger(URL.URL_OCCIDENTGIRL);
                break;
            case "ag"://欧美组合
                getSinger(URL.URL_OCCIDENT);
                break;
            case "jm"://日韩男歌手
                getSinger(URL.URL_JKBOY);
                break;
            case "jf"://日韩女歌手
                getSinger(URL.URL_JKGIRL);
                break;
            case "jg"://日韩组合
                getSinger(URL.URL_JK);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.singer_top:
                getSinger(URL.URL_TOP);
                singer_item_name.setVisibility(View.VISIBLE);
                singer_sex.setVisibility(View.GONE);
                singer_item_name.setText(R.string.singer_top_rangk);
                break;
            case R.id.singer_recently_hot:
                getSinger(URL.URL_RECENTLY);
                singer_item_name.setText(R.string.singer_up_fast);
                singer_item_name.setVisibility(View.VISIBLE);
                singer_sex.setVisibility(View.GONE);
                break;
            case R.id.singer_chinese:
                country = "c";
                showList();
                break;
            case R.id.singer_western:
                country = "a";
                showList();
                break;
            case R.id.singer_Janpan_South_Korea:
                country = "j";
                showList();
                break;

            case R.id.singer_man:
                sex = "m";
                showList();
                break;
            case R.id.singer_women:
                sex = "f";
                showList();
                break;
            case R.id.singer_group:
                sex = "g";
                showList();
                break;
            case R.id.singer_back:
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(android.R.id.content, new FindFragment()).commit();
                finish();
                break;
            case R.id.search:
                Intent intent = new Intent(SingerActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SingerRoot.DataEntity dataEntity = (SingerRoot.DataEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(SingerActivity.this, SingerDetailActivity.class);
        intent.putExtra("singer_id", dataEntity.getSinger_id());
        startActivity(intent);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

}
